<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on">

      <div class="title-container">
        <h3 class="title">项目管理系统</h3>
      </div>

      <el-form-item prop="username">
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="账号"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
          clearable
        >
          <svg-icon slot="prefix" icon-class="user" />
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          show-password
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" />
        </el-input>
      </el-form-item>

      <div>
        <el-checkbox checked>记住密码</el-checkbox>

        <el-link type="primary" style="float: right" @click="toRegister">立即注册</el-link>
      </div>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; height: 47px;margin: 30px 0;"
        @click.native.prevent="handleLogin"
      >登录
      </el-button>

      <div class="tips">
        <span> Copyright © 2021 Yukino All Rights Reserved.</span>
      </div>

    </el-form>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/auth'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('账号不能为空'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码至少为6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '1392175015',
        password: '123456'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password'
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          login(this.loginForm).then(response => {
            const res = response.data
            setToken(res.tokenValue)
            this.$router.push({ path: this.redirect || '/dashboard' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    toRegister() {
      this.$router.push({ path: '/register' })
    }
  }
}
</script>

<style lang="scss" scoped>
$bg: #ffffff;
$dark_gray: #889aa4;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url(~@/assets/background_images/login_bg.png) no-repeat fixed;
  background-size: cover;
  overflow: hidden;

  .login-form {
    background-color: $bg;
    width: 424px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -56%);
    padding: 32px 32px;
    border-radius: 10px;
    box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3);
  }

  .tips {
    font-size: 14px;
    color: #A6A6A8;
    margin-bottom: 10px;
    text-align: center;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: #707070;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
}
</style>

